package com.xinsilu.hospital.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinsilu.hospital.entity.AuthsysUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;


/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ym
 * @since 2021-11-30
 */
public interface AuthsysUserMapper extends BaseMapper<AuthsysUser> {
    @Delete({
            "delete from authsys_user",
            "where ur_id = #{urId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer urId);

    @Insert({
            "insert into authsys_user (ur_id, ur_user_name, ",
            "ur_password, ur_salt, ",
            "introduction, avatar, ",
            "name)",
            "values (#{urId,jdbcType=INTEGER}, #{urUserName,jdbcType=VARCHAR}, ",
            "#{urPassword,jdbcType=VARCHAR}, #{urSalt,jdbcType=INTEGER}, ",
            "#{introduction,jdbcType=VARCHAR}, #{avatar,jdbcType=VARCHAR}, ",
            "#{name,jdbcType=VARCHAR})"
    })
    int insert(AuthsysUser record);

    @InsertProvider(type=AuthSysUserSqlProvider.class, method="insertSelective")
    int insertSelective(AuthsysUser record);

    @Select({
            "select",
            "ur_id, ur_user_name, ur_password, ur_salt, introduction, avatar, name",
            "from authsys_user",
            "where ur_id = #{urId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="ur_id", property="urId", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="ur_user_name", property="urUserName", jdbcType=JdbcType.VARCHAR),
            @Result(column="ur_password", property="urPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column="ur_salt", property="urSalt", jdbcType=JdbcType.INTEGER),
            @Result(column="introduction", property="introduction", jdbcType=JdbcType.VARCHAR),
            @Result(column="avatar", property="avatar", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    AuthsysUser selectByPrimaryKey(Integer urId);

    @UpdateProvider(type=AuthSysUserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(AuthsysUser record);

    @Update({
            "update authsys_user",
            "set ur_user_name = #{urUserName,jdbcType=VARCHAR},",
            "ur_password = #{urPassword,jdbcType=VARCHAR},",
            "ur_salt = #{urSalt,jdbcType=INTEGER},",
            "introduction = #{introduction,jdbcType=VARCHAR},",
            "avatar = #{avatar,jdbcType=VARCHAR},",
            "name = #{name,jdbcType=VARCHAR}",
            "where ur_id = #{urId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(AuthsysUser record);

    @Results(id="UserMap", value = {
            @Result(column="ur_id", property="urId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="ur_user_name", property="urUserName", jdbcType=JdbcType.VARCHAR),
            @Result(column="ur_password", property="urPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column="ur_salt", property="urSalt", jdbcType=JdbcType.INTEGER),
            @Result(column="introduction", property="introduction", jdbcType=JdbcType.VARCHAR),
            @Result(column="avatar", property="avatar", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),

            @Result(column="ur_id", property = "roleList", many = @Many(select = "com.xinsilu.hospital.mapper.AuthsysRoleMapper.selectByUrId"))
    })
    @Select("select * from authsys_user where ur_user_name=#{username}")
    AuthsysUser selectByUserName(String username);

    @ResultMap(value = "UserMap")
    @Select("select * from authsys_user")
    List<AuthsysUser> selectAll();


    @Delete("delete from authsys_ur_rl where ur_id=#{urId}")
    void unassignRoles(Integer urId);

    @Insert("insert into authsys_ur_rl values (#{urId}, #{rlId})")
    void assignRole(@Param("urId") Integer urId,@Param("rlId") Integer rlId);
}
